<?php
/**
 * csv数据导出.
 * 
 * @author  莫 念 
 * @version 1.0
 * @package main
 */


class ExprotCsv  {
	
	public $mimetype = 'text/comma-separated-values';
	public $ext = '.csv';

	private $fp;
	public function i($strInput) {
		if(class_exists('Chinese')){
			$chinese = new Chinese();
			$chinese->Chineses('UTF-8','GB2312');
			return $chinese->Convert($strInput);
		}else{
			return iconv('UTF-8','GB2312//IGNORE',$strInput);//页面编码为utf-8时使用，否则导出的中文为乱码
			//return mb_convert_encoding($strInput,'GB2312','UTF-8');
		}
	}

	public function exprotHead($head = array()){
		$this->fp = fopen('php://output', 'a');
		if($head){
			foreach ($head as $i => $v) {
			// CSV的Excel支持GBK编码，一定要转换，否则乱码
				$head[$i] = $this->i($v);
			}
			fputcsv($this->fp, $head);
		}
	}

	public function exprotData($data = array()){
		// 计数器
		$cnt = 0;
		// 每隔$limit行，刷新一下输出buffer，不要太大，也不要太小
		$limit = 100000;
		if($data){
			foreach($data as $i => $v ){
				$cnt ++;
				if ($limit == $cnt) { //刷新一下输出buffer，防止由于数据过多造成问题
					ob_flush();
					flush();
					$cnt = 0;
				}
				$data[$i] = $this->i($v)."\t";
			}
			//echo '<pre>';
			//print_r($data);exit;
			fputcsv($this->fp, $data);
			unset($data);
		}
	}

	public function exprotFoot(){
		  fclose($this->fp);
		  exit;
	
	} 


	function GetGB2312String($name){
		$tostr = "";
		for($i=0;$i<strlen($name);$i++)
		{
		   $curbin = ord(substr($name,$i,1));
		   if($curbin < 0x80)
		   {
			$tostr .= substr($name,$i,1);
		   }elseif($curbin < bindec("11000000")){
			$str = substr($name,$i,1);
			$tostr .= "&#".ord($str).";";
		   }elseif($curbin < bindec("11100000")){
			$str = substr($name,$i,2);
			$tostr .= "&#".$this->GetUnicodeChar($str).";";
			$i += 1;
		   }elseif($curbin < bindec("11110000")){
			$str = substr($name,$i,3);
			$gstr= iconv("UTF-8","GB2312",$str);
			if(!$gstr)
			{
			$tostr .= "&#".$this->GetUnicodeChar($str).";";
			}else{
			$tostr .= $gstr;
			}
			$i += 2;
		   }elseif($curbin < bindec("11111000")){
			$str = substr($name,$i,4);
			$tostr .= "&#".$this->GetUnicodeChar($str).";";
		   
			$i += 3;
		   }elseif($curbin < bindec("11111100")){
			$str = substr($name,$i,5);
			$tostr .= "&#".$this->GetUnicodeChar($str).";";
			$i += 4;
		   }else{
			$str = substr($name,$i,6);
			$tostr .= "&#".$this->GetUnicodeChar($str).";";
			$i += 5;
		   }
		}
		return $tostr;
	}//end function
	function GetUnicodeChar($str){
		$temp = "";
		for($i=0;$i<strlen($str);$i++)
		{
		   $x = decbin(ord(substr($str,$i,1)));
		   if($i == 0)
		   {
			$s = strlen($str)+1;
			$temp .= substr($x,$s,8-$s);
		   }else{
			$temp .= substr($x,2,6);
		   }
		}
		return bindec($temp);
	}//end function

	 public  function safeEncoding($string,$outEncoding ='UTF-8') {  
        $encoding = "UTF-8";  
        for($i=0;$i<strlen($string);$i++){  
            if(ord($string{$i})<128)  
            continue;  
            if((ord($string{$i})&224)==224){  
                //第一个字节判断通过  
                $char = $string{++$i};  
                if((ord($char)&128)==128){  
                    //第二个字节判断通过  
                    $char = $string{++$i};  
                    if((ord($char)&128)==128)  
                    {  
                        $encoding = "UTF-8";  
                        break;  
                    }  
                }  
            }  
            if((ord($string{$i})&192)==192){  
                //第一个字节判断通过  
                $char = $string{++$i};  
                if((ord($char)&128)==128){  
                    // 第二个字节判断通过  
                    $encoding = "GB2312";  
                    break;  
                }  
            }  
        }  
           
        if(strtoupper($encoding) == strtoupper($outEncoding))  
			return $string;  
        else  
			return iconv($encoding,$outEncoding,$string);  
    }

}
